//
//  StorageAccessor.h
//  lib
//

//
#import "CacheController.h"


@interface CacheController(private)
- (void)initMember;
- (void)releaseMember;

@end

@implementation CacheController
@synthesize m_CurrentDirType;


- (void)initMember
{
	m_CurrentDirType = STORAGE_ACCESS_DIR_CACHES;
	m_DirPathTbl = nil;
}

- (id)init
{
	if( self = [super init] )	
	{
		[self initMember];
		
		
		NSString* tmp_path = NSTemporaryDirectory();
		NSArray* caches_dir = NSSearchPathForDirectoriesInDomains( NSCachesDirectory, NSUserDomainMask, YES );
		NSString* caches_path = [caches_dir objectAtIndex:0];
		NSArray* doc_dir = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
		NSString* doc_path = [doc_dir objectAtIndex:0];
		
		m_DirPathTbl = [[NSArray alloc] initWithObjects:caches_path, doc_path, tmp_path, nil];
	}
	return self;
}



- (void)releaseMember
{
	SAFE_RELEASE(m_DirPathTbl)
}

- (void)dealloc
{
	[self releaseMember];
	[self initMember];
	
	[super dealloc];
}



- (NSString*)getAccessPath
{
	if( m_CurrentDirType < [m_DirPathTbl count] )
		return [m_DirPathTbl objectAtIndex:m_CurrentDirType];
	
#ifdef __DEBUG__
	DebLog(@"currentDirType error:%d", m_CurrentDirType);
	exit(-1);
#endif
	return [m_DirPathTbl objectAtIndex:0];
}


- (BOOL)save:(NSData*)data path:(NSString*)path
{
	BOOL ret = NO;
	
	NSString* access_path = [self getAccessPath];
	if( access_path )
	{
		NSString* save_path = [NSString stringWithFormat:@"%@/%@", access_path, path];
		
		ret = [data writeToFile:save_path atomically:YES];
		if( !ret )
			DebLog(@"save error:%@", save_path);
	}
	return ret;
}
- (BOOL)saveArray:(NSArray*)data path:(NSString*)path
{
	BOOL ret = NO;
	
	NSString* access_path = [self getAccessPath];
	if( access_path )
	{
		NSString* save_path = [NSString stringWithFormat:@"%@/%@", access_path, path];
		
		ret = [data writeToFile:save_path atomically:YES];
		if( !ret )
			DebLog(@"save error:%@", save_path);
	}
	return ret;
}
- (BOOL)saveDictionary:(NSDictionary*)data path:(NSString*)path
{
	BOOL ret = NO;
	
	NSString* access_path = [self getAccessPath];
	if( access_path )
	{
		NSString* save_path = [NSString stringWithFormat:@"%@/%@", access_path, path];
		
		ret = [data writeToFile:save_path atomically:YES];
		if( !ret )
			DebLog(@"save error:%@", save_path);
	}
	return ret;
}



- (NSData*)load:(NSString*)path
{
	NSData* ret = nil;
	
	NSString* access_path = [self getAccessPath];
	if( access_path )
	{
		NSString* load_path = [NSString stringWithFormat:@"%@/%@", access_path, path];
		
		ret = [NSData dataWithContentsOfFile:load_path];
		if( !ret )
			DebLog(@"load error:%@", load_path);
	}
	return ret;
}
- (NSArray*)loadArray:(NSString*)path
{
	NSArray* ret = nil;
	
	NSString* access_path = [self getAccessPath];
	if( access_path )
	{
		NSString* load_path = [NSString stringWithFormat:@"%@/%@", access_path, path];
		
		ret = [NSArray arrayWithContentsOfFile:load_path];
		if( !ret )
			DebLog(@"load error:%@", load_path);
	}
	return ret;
}
- (NSDictionary*)loadDictionary:(NSString*)path
{
	NSDictionary* ret = nil;
	
	NSString* access_path = [self getAccessPath];
	if( access_path )
	{
		NSString* load_path = [NSString stringWithFormat:@"%@/%@", access_path, path];
		
		ret = [NSDictionary dictionaryWithContentsOfFile:load_path];
		if( !ret )
			DebLog(@"load error:%@", load_path);
	}
	return ret;
}


- (BOOL)isExist:(NSString*)path
{
	NSString* access_path = [self getAccessPath];
	if( access_path )
	{
		NSString* check_path = [NSString stringWithFormat:@"%@/%@", access_path, path];
		
		NSFileManager* file_mgr = [NSFileManager defaultManager];
		return [file_mgr fileExistsAtPath:check_path isDirectory:nil];
	}
	return NO;
}



- (BOOL)createDirectory:(NSString*)path
{
	
	if( [self isExist:path] )
		return YES;
	
	BOOL ret = NO;
	NSString* access_path = [self getAccessPath];
	if( access_path )
	{
		NSString* dir_path = [NSString stringWithFormat:@"%@/%@", access_path, path];
		
		NSFileManager* file_manager = [NSFileManager defaultManager];
//deprecated iOS 4.0
//		ret = [file_manager createDirectoryAtPath:dir_path attributes:nil];
		ret = [file_manager createDirectoryAtPath:dir_path 
					  withIntermediateDirectories:YES
									   attributes:nil
											error:NULL];
		if( !ret )
			DebLog(@"make dir error:%@", dir_path);
	}
	return ret;
}


- (unsigned long long)getDirectorySize:(NSString*)path
{
	unsigned long long size = 0;
	NSArray* dirArray = [[NSFileManager defaultManager] subpathsAtPath:path];
	for( NSString* file_name in dirArray )
	{
//deprecated iOS 4.0
//		NSDictionary *attr = [[NSFileManager defaultManager] fileAttributesAtPath:[path stringByAppendingPathComponent:file_name] traverseLink:NO];
		NSDictionary* attr = [[NSFileManager defaultManager] attributesOfItemAtPath:[path stringByAppendingPathComponent:file_name]
																			  error:NULL];
		size += [[attr objectForKey:NSFileSize] unsignedLongLongValue];
	}
	return size;
}


- (BOOL)move:(NSString*)src_path toPath:(NSString*)dst_path
{
	NSString* access_path = [self getAccessPath];
	if( !access_path )
		return NO;
	
	
	if( ![self isExist:src_path] )
		return NO;
	
	NSFileManager* file_manager = [NSFileManager defaultManager];
	NSString* from = [NSString stringWithFormat:@"%@/%@", access_path, src_path];
	NSString* to = [NSString stringWithFormat:@"%@/%@", access_path, dst_path];
	
	
	return [file_manager moveItemAtPath:from toPath:to error:nil];
}



- (BOOL)copy:(NSString*)src_path toPath:(NSString*)dst_path
{
	NSString* access_path = [self getAccessPath];
	if( !access_path )
		return NO;
	
	
	if( ![self isExist:src_path] )
		return NO;
	
	NSFileManager* file_manager = [NSFileManager defaultManager];
	NSString* from = [NSString stringWithFormat:@"%@/%@", access_path, src_path];
	NSString* to = [NSString stringWithFormat:@"%@/%@", access_path, dst_path];
	
	
	return [file_manager copyItemAtPath:from toPath:to error:nil];
}


- (BOOL)remove:(NSString*)path
{
	BOOL ret = NO;
	
	NSString* access_path = [self getAccessPath];
	if( access_path )
	{
		NSString* del_path = [NSString stringWithFormat:@"%@/%@", access_path, path];
		NSFileManager* file_manager = [NSFileManager defaultManager];
		
//#ifdef __DEBUG__
//		NSArray* list = [file_manager directoryContentsAtPath:access_path];
//		NSArray* list2 = [file_manager directoryContentsAtPath:del_path];
//		if( list && list2 )
//		{
//			DebLog([list description]);
//			DebLog([list2 description]);
//		}
//#endif	//__DEBUG__
		
		
		ret = [file_manager removeItemAtPath:del_path error:nil];
		if( !ret )
			DebLog(@"delete error:%@", del_path);
	}
	return ret;
}

- (void)removeAll
{
	NSString* access_path = [self getAccessPath];
	if( access_path )
	{
		NSFileManager* file_manager = [NSFileManager defaultManager];
//deprecated iOS 4.0
//		NSArray* list = [file_manager directoryContentsAtPath:access_path];
		NSArray* list = [file_manager contentsOfDirectoryAtPath:access_path error:NULL];
		for( NSString* name in list )
		{
		
			NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
			{
				[self remove:name];
			}
			SAFE_RELEASE(pool)
		}
	}
}


- (unsigned long long)getFileSize:(NSString*)path
{
	NSString* access_path = [self getAccessPath];
	if( access_path )
	{
		NSString* check_path = [NSString stringWithFormat:@"%@/%@", access_path, path];
		
		NSFileManager* file_mgr = [NSFileManager defaultManager];
		
//deprecated iOS4.0
//		NSDictionary* attr = [file_mgr fileAttributesAtPath:check_path traverseLink:NO];
		NSDictionary* attr = [file_mgr attributesOfItemAtPath:check_path error:NULL];
		
		unsigned long long size = [[attr objectForKey:NSFileSize] unsignedLongLongValue];
		
		return size;
	}
	return 0;
}


- (NSString *)getFileModifyDateString:(NSString*)path
{
	NSString* access_path = [self getAccessPath];
	if( access_path )
	{
		NSString* check_path = [NSString stringWithFormat:@"%@/%@", access_path, path];
		
		NSFileManager* file_mgr = [NSFileManager defaultManager];
		
//deprecated iOS4.0
//		NSDictionary* attr = [file_mgr fileAttributesAtPath:check_path traverseLink:NO];
		NSDictionary* attr = [file_mgr attributesOfItemAtPath:check_path error:NULL];
		
		//by m.w
//		NSString* date = [attr objectForKey:NSFileModificationDate];
		NSString* date = [[attr objectForKey:NSFileModificationDate] description];
		
		DebLog(@"file:%@ modify:%@", check_path, date);
		
		return date;
	}
	
	return nil;
}

- (NSArray*)getDirectories:(NSString*)path
{
	NSString* access_path = [self getAccessPath];
	NSArray* result = nil;
	if( access_path )
	{
		NSString* check_path = [NSString stringWithFormat:@"%@/%@", access_path, path];
		NSFileManager* file_mgr = [NSFileManager defaultManager];
		result =[file_mgr contentsOfDirectoryAtPath:check_path
											  error:nil];
		//result = [file_mgr directoryContentsAtPath:check_path];
	}
	
	return result;
}

- (NSDate*)getFileModifyDate:(NSString*)path
{
	NSString* access_path = [self getAccessPath];
	if( access_path )
	{
		NSString* check_path = [NSString stringWithFormat:@"%@/%@", access_path, path];
		
		NSFileManager* file_mgr = [NSFileManager defaultManager];
		NSDictionary* attr = [file_mgr attributesOfItemAtPath:check_path error:NULL];
		NSDate* date = [attr objectForKey:NSFileModificationDate];
		
		DebLog(@"file:%@ modify:%@", check_path, date);
		
		return date;
	}
	
	return nil;
}

@end